package com.example.android.locrec;

import java.nio.FloatBuffer;
import java.util.List;

import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.RectF;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.Projection;

public class PositionOverlay extends Overlay {

    private double mCurrentLatitude;
    private double mCurrentLongitude;

    private boolean displayActionHistory = false;
    private List<GPSRecord> locationRecordList;

    /**
     * 緯度経度を設定する
     */
    public void setCurrentLocation(double latitude, double longitude) {
        mCurrentLatitude = latitude;
        mCurrentLongitude = longitude;
    }

    /**
     * 行動履歴を表示するか設定する
     * 
     * @param displayActionHistory
     */
    public void setDisplayActionHistory(boolean displayActionHistory) { // (1)
                                                                        // from
        this.displayActionHistory = displayActionHistory;
    }

    /**
     * 行動履歴を表示しているか
     * 
     * @return
     */
    public boolean isDisplayActionHistory() {
        return displayActionHistory;
    }

    /*
     * 行動履歴を設定する
     */
    public void setActionHistory(List<GPSRecord> list) {
        locationRecordList = list;
    } // (1) to

    @Override
    public void draw(Canvas canvas, MapView mapView, boolean shadow) {

        // 現在地を描画
        drawCurrentLocation(canvas, mapView, shadow);
        
        // 履歴を表示
        if (displayActionHistory) { // (1) from
            drawActionHistory(canvas, mapView, shadow);
        } // (1) to

        super.draw(canvas, mapView, shadow);
    }

    private final int mRadius = 5;

    /**
     * 現在地を描画する
     */
    private void drawCurrentLocation(Canvas canvas, MapView mapView,
            boolean shadow) {

        if (shadow == false) {

            Projection projection = mapView.getProjection();

            Double lat = mCurrentLatitude * 1E6;
            Double lon = mCurrentLongitude * 1E6;

            // GeoPointを作成
            GeoPoint geoPoint = new GeoPoint(lat.intValue(), lon
                    .intValue());

            // 画面の解像度にあわせて描画位置を変換する
            Point point = new Point();
            projection.toPixels(geoPoint, point);

            // 描画範囲設定
            RectF oval = new RectF(point.x - mRadius, point.y - mRadius,
                    point.x + mRadius, point.y + mRadius);

            // 背景を設定
            RectF backRect = new RectF(point.x + 2 + mRadius, point.y - 3
                    * mRadius, point.x + 65, point.y + mRadius);

            // 描画設定
            Paint paint = new Paint();
            paint.setARGB(255, 180, 180, 180);
            paint.setAntiAlias(true);
            paint.setFakeBoldText(true);

            Paint backPaint = new Paint();
            backPaint.setARGB(230, 50, 50, 50);
            backPaint.setAntiAlias(true);

            // 現在地を描画
            canvas.drawOval(oval, paint);
            canvas.drawRoundRect(backRect, 5, 5, backPaint);
            canvas.drawText("現在地", point.x + 2 * mRadius, point.y, paint);
        }
    }

    @Override
    public boolean onTap(GeoPoint point, MapView mapView) {
        return false;
    }

    /**
     * 行動履歴を描画する
     * 
     * @param canvas
     * @param mapView
     * @param shadow
     */
    private void drawActionHistory(Canvas canvas, MapView mapView, // (2) from
            boolean shadow) {

        Projection projection = mapView.getProjection();

        // 描画するペンを設定
        Paint paint = new Paint();
        paint.setARGB(128, 0, 255, 0);
        paint.setStrokeWidth(3.0f);
        paint.setAntiAlias(true);

        int cnt = 0;
        float oldpx = 0, oldpy = 0;
        for (GPSRecord loc : locationRecordList) {

            double lat = loc.getLatitude() * 1E6;
            double lon = loc.getLongitude() * 1E6;

            GeoPoint geoPoint = new GeoPoint((int) lat, (int) lon);

            // 画面の解像度にあわせて位置を変換する
            Point point = new Point();
            projection.toPixels(geoPoint, point);

            if (cnt > 0) {
                // 線を描画する
                canvas.drawLine(oldpx, oldpy, point.x, point.y, paint);
            }
            
            cnt++;
            oldpx = point.x;
            oldpy = point.y;
        }
    } // (2) to
}
